package com.neutech.shop.controller;

import com.neutech.shop.entity.Category;
import com.neutech.shop.service.CategoryService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Neuedu-远哥
 * @version 1.0
 * @date 2021/9/27 9:35
 */
@Controller
@RequestMapping("/back/category")
public class CategoryController {
    @Resource
    private CategoryService service;

    @RequestMapping(value = "/insertRootCategory",method = RequestMethod.POST)
    public String insertRootCategory(String name,String descr){
        service.insertRootCategory(name,descr);
        return "redirect:category_list.jsp";
    }
    @ResponseBody
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public List<Category> selectAll(){
        return service.selectAll();
    }

    /**
     * 添加子类别
     * @param id
     * @return
     */
    @RequestMapping(value = "/insertChildLoad",method = RequestMethod.GET)
    public String insertChildLoad(ModelMap map,Integer id){

        Category category = service.selectByParentId(id);
        System.out.println(category);
        map.addAttribute("category",category);
        return "category_insertChild.jsp";
    }
    @RequestMapping(value ="/insertChild",method = RequestMethod.POST)
    public String insertChild(String name,String descr,Integer pid){

        service.insertChildCategory(name,descr,pid);
        return "redirect:category_list.jsp";
    }
}
